package handler

import (
	"encoding/json"
	"github.com/gin-gonic/gin"
	"piggy-charm/cache"
	"piggy-charm/moderl"
	"strconv"
	"time"
)

func RoleNodeAdd(c *gin.Context) {
	uid, _ := strconv.Atoi(c.PostForm("uid"))
	rid, _ := strconv.Atoi(c.PostForm("rid"))

	if uid == 0 || rid == 0 {
		c.JSON(200, gin.H{
			"code": 500,
			"msg":  "内容不能为空",
		})
		return
	}

	data := moderl.RoleNode{
		Uid: uid,
		Rid: rid,
	}

	moderl.DB.Create(&data)
	c.JSON(200, gin.H{
		"code": 500,
		"msg":  "添加成功",
	})
	return
}
func RoleNodeDetail(c *gin.Context) {
	/*
				SELECT
		  *
		FROM
		  nodes
		  LEFT JOIN role_nodes ON nodes.id = role_nodes.rid
		WHERE
		  role_nodes.uid = 1
	*/
	uid := c.Query("uid")
	var list []moderl.Node
	moderl.DB.Table("nodes").
		Joins(" LEFT JOIN role_nodes ON nodes.id = role_nodes.rid").
		Where(" role_nodes.uid = ?", uid).Debug().
		Find(&list)

	c.JSON(200, gin.H{
		"code": 200,
		"data": list,
		"msg":  "okok",
	})
	return
}
func UserRoleDetail(c *gin.Context) {
	/*
		SELECT
		  *
		FROM
		  roles
		  LEFT JOIN user_roles ON roles.id = user_roles.rid
		WHERE
		  user_roles.uid = 1
	*/
	uid := c.Query("uid")

	var list []moderl.Role
	moderl.DB.Table("roles").
		Joins("LEFT JOIN user_roles ON roles.id = user_roles.rid").
		Where("user_roles.uid = ?", uid).
		Find(&list)
	c.JSON(200, gin.H{
		"code": 200,
		"data": list,
		"msg":  "okok",
	})
	return

}
func CarList(c *gin.Context) {
	/*
		SELECT
		  cars.car_no AS CarNo,
		  cars.car_name AS CarName,
		  cars.car_time AS CarTime,
		  car_dds.dd_no as DdNo,
		  car_dds.dd_fh AS DdFh,
		  car_dds.dd_sh AS DdSh
		FROM
		  cars
		  LEFT JOIN car_dds ON cars.id = car_dds.car_id
	*/

	page, _ := strconv.Atoi(c.DefaultQuery("page", "1"))
	size, _ := strconv.Atoi(c.DefaultQuery("size", "3"))

	offset := (page - 1) * size
	var list []moderl.CarDdList
	redis_key := "list_id" + strconv.Itoa(page)
	cacheData, _ := cache.Rdb.Get(cache.Ctx, redis_key).Result()
	if cacheData == "" {
		moderl.DB.Select(
			"cars.car_no AS CarNo",
			"cars.car_name AS CarName",
			"cars.car_time AS CarTime",
			"car_dds.dd_no as DdNo",
			"car_dds.dd_fh AS DdFh",
			"car_dds.dd_sh AS DdSh",
		).Table("cars").
			Joins("LEFT JOIN car_dds ON cars.id = car_dds.car_id").
			Offset(offset).
			Limit(size).
			Find(&list)
		jsonData, _ := json.Marshal(&list)
		cache.Rdb.Set(cache.Ctx, redis_key, jsonData, time.Hour*2)
	} else {
		json.Unmarshal([]byte(cacheData), &list)
	}
	var total int64
	moderl.DB.Model(moderl.Car{}).Count(
		&total)
	c.JSON(200, gin.H{
		"code":  200,
		"data":  list,
		"total": total,
	})

}
func CarDetail(c *gin.Context) {
	id := c.Query("id")
	var list moderl.CarDdList
	redis_key := "detail_id" + id
	cacheData, _ := cache.Rdb.Get(cache.Ctx, redis_key).Result()
	if cacheData == "" {
		moderl.DB.Select(
			"cars.car_no AS CarNo",
			"cars.car_name AS CarName",
			"cars.car_time AS CarTime",
			"car_dds.dd_no as DdNo",
			"car_dds.dd_fh AS DdFh",
			"car_dds.dd_sh AS DdSh",
		).Table("cars").
			Joins("LEFT JOIN car_dds ON cars.id = car_dds.car_id").
			Where("car_dds.car_id = ?", id).
			Find(&list)
		jsonData, _ := json.Marshal(&list)
		cache.Rdb.Set(cache.Ctx, redis_key, jsonData, time.Hour*2)
	} else {
		json.Unmarshal([]byte(cacheData), &list)
	}
	c.JSON(200, gin.H{
		"code": 200,
		"data": list,
	})
}
